layui.use(['laydate','form','table','laytpl','layer'], function(){
	var laydate = layui.laydate;
	var form = layui.form;
	var table = layui.table;
	var laytpl = layui.laytpl;
	var layer = layui.layer;
	var type = $('.activity-btn').attr('lay-event')
	/*@表格数据处理*/
	table_data = function(){
		console.log(type)
		if(type == 0){/*拜访任务*/
			return [[ //表头
		    	{field: 'user_id', title: '序号'},
		    	{field: 'visit_t_text', title: '拜访时间'},
		    	{field: 'user_sn', title: '客户编号'},
		    	{title: '照片', toolbar: '<div><img src="{{d.user_img}}" width="50" height="50"></div>'},
		    	{field: 'username', title: '姓名'},
		    	{field: 'nkname', title: '称呼'},
		    	{field: 'sex', title: '性别'},
		    	{field: 'age', title: '年龄'},
		    	{field: 'phone', title: '联系方式'},
		    	{field: 'spare_phone', title: '备用联系方式'},
		    	{field: 'u_type', title: '客户类型'},
		    	{title: '订单记录', width: 90, toolbar: '<div><a class="layui-btn" onclick="xadmin.open(\'订单记录\',\'/user/order/index/user_sn/{{d.user_sn}}\')">订单记录</a></div>'},
		    	{title: '咨询记录', width: 90, toolbar: '<div><a class="layui-btn" onclick="xadmin.open(\'咨询记录\',\'/user/consult/index/user_sn/{{d.user_sn}}\')">咨询记录</a></div>'},
		    	{title: '维修记录', width: 90, toolbar: '<div><a class="layui-btn" onclick="xadmin.open(\'维修记录\',\'/user/repair/index/user_sn/{{d.user_sn}}\')">维修记录</a></div>'},
		    	{title: '操作', width: 100,toolbar: '<div><a class="layui-btn" onclick="xadmin.open(\'拜访记录\',\'/user/visit/add/ty/1/user_id/{{d.user_id}}\')">完成拜访</a></div>'},
		    ]]
		}else if(type == 1){/*跟单任务*/

		}else if(type == 2){/*自定义任务*/
			return [[ //表头
		    	{field: 'visit_log_id', title: '序号'},
		    	{field: 'cus_time', title: '拜访时间'},
		    	{field: 'content', title: '拜访内容'},
		    	{title: '操作', width: 200,toolbar: '#table-btn'},
		    ]]
		}
	}
	/*@初始化table*/
	layui.tableObj = table.render({
	    elem: '#table-list',
	    id: 'list',
	    url: '/task/task/index', //数据接口
	    page: true, //开启分页
	    where: {ty: type},
	    cols: table_data(),
	});
	
	/*@删除请求*/
	del = function(obj_data){
		$.post('/task/task/del', obj_data, function(data){
			if(data.code !== 1){
				layer.msg(data.msg, {icon: 5})
			}else{
				layer.msg(data.msg, {icon: 6, time: 1000}, function(){
					layui.tableObj.reload()
				})
			}
		})
	}
	/*@完成任务*/
	finish_fun = function(obj_data){
		layer.confirm('是否完成任务', function(){
			$.post('/task/task/finish', obj_data, function(data){
				if(data.code !== 1){
					layer.msg(data.msg, {icon: 5})
				}else{
					layer.msg(data.msg, {icon: 6, time: 1000}, function(){
						layui.tableObj.reload()
					})
				}
			})
		})
	}
	/*@监听工具*/
	table.on('tool(list)', function(obj_tr){ //注：tool是工具条事件名，test是table原始容器的属性 lay-filter="对应的值"
	  	var data = obj_tr.data; //获得当前行数据
	  	var layEvent = obj_tr.event; //获得 lay-event 对应的值（也可以是表头的 event 参数对应的值）
	  	var tr = obj_tr.tr; //获得当前行 tr 的DOM对象
	  	if(layEvent === 'del'){ //删除
	  		let obj_data = {}
	  		obj_data.id = data.visit_log_id
	  		del(obj_data)
	  	}else if(layEvent === 'ok'){ //删除
	  		let obj_data = {}
	  		obj_data.id = data.visit_log_id
	  		finish_fun(obj_data)
	  	}
	});
	/*@查询*/
	form.on('submit(search)',function(data){
		$(data.elem).addClass('activity-btn');
		$(data.elem).removeClass('box-btn');
		$(data.elem).siblings().addClass('box-btn');
		$(data.elem).siblings().removeClass('activity-btn');
		type = $(data.elem).attr('lay-event');
		/*重置表格数据*/
		layui.tableObj = table.render({
		    elem: '#table-list',
		    id: 'list',
		    url: '/task/task/index', //数据接口
		    page: true, //开启分页
		    where: {ty: type},
		    cols: table_data(),
		});
        return false
	})
	
});